// pages/base_mine/recycle_address/add.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        liaison_name: '',
        liaison_phone: '',
        position: null,
        position_marker: [],
        address: null,
        address_detail: '',
        address_describe: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录状态
        app.verifyLogin();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

        // 初始化QQ地图
        app.initQQmap();

    },

    /**
     * 更改输入框
     */
    changeInput: function(e) {
        let change = new Object();
        change[e.target.dataset.name] = e.detail.value.replace(/[\n\r]/g, '');
        this.setData(change);
    },

    /**
     * 配置地图
     */
    makePosition: function(e) {
        // 当前页面
        let page = this;
        // 确认权限
        wx.showLoading({
            title: '定位中...',
        });
        wx.authorize({
            scope: "scope.userLocation",
            success: function(e) {
                // 权限正确，定位地点
                wx.chooseLocation({
                    success: function (e) {
                        let address_detail = e.name;
                        app.utils.qqmap.reverseGeocoder({
                            location: {
                                latitude: e.latitude,
                                longitude: e.longitude
                            },
                            coord_type: 5,
                            get_poi: 0,
                            sig: app.utils.qqmap_sk,
                            success: function(e) {
                                wx.hideLoading();
                                // 保存更改
                                let change = {
                                    position: {
                                        latitude: e.result.location.lat,
                                        longitude: e.result.location.lng,
                                    },
                                    position_marker: [{
                                        latitude: e.result.location.lat,
                                        longitude: e.result.location.lng,
                                        width: 50,
                                        height: 50
                                    }],
                                    address: {
                                        province: e.result.ad_info.province,
                                        city: e.result.ad_info.city,
                                        district: e.result.ad_info.district,
                                    },
                                    address_detail: address_detail,
                                };
                                console.log(change);
                                page.setData(change);
                            },
                            fail: function(e) {
                                wx.showToast({
                                    title: '腾讯地图返回失败',
                                    icon: 'none'
                                });
                            }
                        });
                    },
                    fail: function(e) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '定位失败',
                            icon: 'none',
                        });
                    }
                });
            },
            fail: function(e) {
                wx.hideLoading();
                switch(e.errMsg){
                    case 'authorize:fail auth cancel' :
                        wx.showToast({
                            title: '未能获取到定位地址',
                            icon: 'none'
                        });
                        break;
                    default :
                        wx.showModal({
                            title: "提示",
                            content: "由于您禁用了小程序的权限，暂时无法获取位置，请在小程序设置里重新启用",
                            success: function (e) {
                                if (true === e.confirm) {
                                    wx.openSetting();
                                } else {
                                    wx.showToast({
                                        title: '不能获取当前位置',
                                    });
                                };
                            }
                        });
                        break;
                };
                
                
            }
        });
    },

    /**
     * 提交
     */
    submit: function() {

        let data = {
            liaison_name: this.data.liaison_name,
            liaison_phone: this.data.liaison_phone,
            position: this.data.position,
            address: this.data.address,
            address_detail: this.data.address_detail,
            address_describe: this.data.address_describe,
        };

        if (0 === data.liaison_name.length) {
            wx.showToast({
                title: '请输入联系人称呼',
                icon: 'none',
            });
            return;
        };
        if (0 === data.liaison_phone.length) {
            wx.showToast({
                title: '请输入联系人手机号码',
                icon: 'none',
            });
            return;
        };
        if (11 !== data.liaison_phone.length) {
            wx.showToast({
                title: '联系人手机号码不足11位',
                icon: 'none',
            });
            return;
        };
        if (null === data.position) {
            wx.showToast({
                title: '请定位目标地址',
                icon: 'none',
            });
            return;
        };
        if (0 === data.address_describe.length) {
            wx.showToast({
                title: '目标位置名称不能为空',
                icon: 'none',
            });
            return;
        };

        wx.showLoading({
            title: '请等待...' 
        });
        wx.request({
            url: app.data.service_url + 'recycle_address/create',
            header: {
                'Client-Token': app.data.client_token
            },
            data: data,
            method: 'POST',
            success: function(response) {
                wx.hideLoading();
                wx.hideToast();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        wx.showToast({
                            title: '操作成功',
                            mask: true,
                        });
                        setTimeout(function(){
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    } else {
                        wx.showToast({
                            title: '添加失败，' + response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '添加失败，远程服务发生错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });

    }
});